﻿// --------------------------------------------------------
// <copyright file="PluploadController.cs" company="ChiakiYu">
//     Copyright (c) 2015-2017 ChiakiYu.All rights reserved
// </copyright >
// <creation-date>2017-03-29 13:44</creation-date>
// <last-editor>于琦(yu_199012@qq.com)</last-editor>
// <last-date>2017-03-29 16:38</last-date>
// --------------------------------------------------------

using System;
using System.IO;
using System.Web.Mvc;

namespace MvcDemo.Controllers
{
    public class PluploadController : Controller
    {
        public ActionResult Plupload()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Upload()
        {
            var nowTime = DateTime.Now;

            var savePath = $"/Uploads/img/{nowTime.Year}/{nowTime.Month:D2}/{nowTime.Day:D2}/"; //上传文件的路径 

            var localPath = Server.MapPath(savePath);
            if (!Directory.Exists(Path.GetDirectoryName(localPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(localPath));
            }
            var request = System.Web.HttpContext.Current.Request;
            var src = string.Empty;

            if (request.Files.Count <= 0) return Json(new {src, src1 = "111"});
            for (var i = 0; i < request.Files.Count; i++)
            {
                var file = request.Files[i];
                var extension = Path.GetExtension(file.FileName);
                var fileName = $"{DateTime.Now.ToString("HHmmss") + new Random().Next(100000, 999999)}{extension}";
                file.SaveAs(localPath + fileName);
                src = savePath + fileName;
            }
            return Json(new {src, src1 = "111"});
        }
    }
}